package cn.ywrby.service.impl;

import cn.ywrby.domain.*;
import cn.ywrby.mapper.UserMapper;
import cn.ywrby.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;



    @Override
    public List<User> getAllUser() {
        List<User> userList;
        userList = userMapper.findAllUser();
        return userList;
    }

    @Override
    public User verify(User user) {
        String userId=user.getUserId();
        String password= user.getPassword();
        user = userMapper.findUserByUserAndPwd(userId, password);
        if(user!=null){
            user.setPassword(null);
            List<String> userGroup = getUserGroup(userId);
            user.setGroup(userGroup);
            return user;
        }else {
            return null;
        }
    }

    @Override
    public List<String> getUserGroup(String userId) {
        List<Group> userGroup = userMapper.getUserGroup(userId);
        List<String> groupNameList=new ArrayList<>();
        for (Group group:userGroup){
            groupNameList.add(group.getGroupName());
        }
        return groupNameList;
    }

    @Override
    public User findUserById(String userId) {
        User user=userMapper.findUserById(userId);
        List<String> userGroup = getUserGroup(userId);
        user.setGroup(userGroup);
        return user;
    }

    @Override
    public boolean updateUserInfo(String userId, User user) {
        try {
            userMapper.updateUserInfo(userId,user.getUserId(),user.getUsername(),
                    user.getPassword(),user.getEmail(),user.getDepartment(),user.getFace(),user.getPhone());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }



}
